package cn.moningxi.controller;

import cn.moningxi.bean.CCollect;
import cn.moningxi.bean.CustomerInfomation;
import cn.moningxi.bean.OShopCart;
import cn.moningxi.service.CCollectService;
import cn.moningxi.service.OShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class OShopCartController {

    @Autowired
    private OShopCartService oShopCartService;
    @Autowired
    private CCollectService cCollectService;

    /**
     * 获取用户购物车信息
     *
     * @param request
     * @return
     */
    @RequestMapping("orderCartApi/queryAllShopCart")
    public ModelAndView queryAllShopCart(HttpServletRequest request, ModelAndView mv) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        List<OShopCart> oShopCartList = oShopCartService.queryShopCartList(onlineUser.getCustomerId());
        for (OShopCart oShopCart : oShopCartList) {
            CCollect cCollect = cCollectService.queryByCustomerIdAndGoodId(oShopCart.getCustomerId(), oShopCart.getGoodsId());
            oShopCart.setCCollect(cCollect);
        }
        mv.addObject("ShopCartList", oShopCartList);
        mv.setViewName("/business/home/shopcart");
        return mv;
    }

    /**
     * 移除购物车
     *
     * @param mv
     * @return
     */
    @RequestMapping("orderCartApi/removeShopCart")
    public ModelAndView removeShopCart(ModelAndView mv, Integer shopCartId) {

        oShopCartService.removeShopCart(shopCartId);
        mv.setViewName("redirect:/orderCartApi/queryAllShopCart");
        return mv;

    }

    /**
     * 商品页面加入购物车
     *
     * @param mv
     * @param request
     * @param oShopCart
     */
    @RequestMapping("orderCartApi/saveShopCart")
    public ModelAndView saveShopCart(ModelAndView mv, HttpServletRequest request, OShopCart oShopCart) {
        System.out.println(oShopCart);
        CustomerInfomation customerInfomation = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        oShopCart.setCustomerId(customerInfomation.getCustomerId());

        if (oShopCartService.queryShopCartByGoodsIdAndCid(oShopCart.getGoodsId(), customerInfomation.getCustomerId()) == null) {
            oShopCartService.insertShopCart(oShopCart);
        } else {
            oShopCartService.updateShopCartAmount(oShopCart);
        }

        mv.addObject("promptMsg", "加入购物车成功");
        mv.setViewName("redirect:/orderCartApi/queryAllShopCart");
        return mv;
    }

    /**
     * 更新购物车商品数量
     *
     * @param mv
     * @param shopCartId
     * @param amount
     * @return
     */
    @RequestMapping("orderCartApi/upadateShopCart")
    public ModelAndView upadateShopCart(ModelAndView mv, Integer shopCartId, Integer amount) {

        oShopCartService.updateShopCartAmount1(shopCartId, amount);
        mv.setViewName("redirect:/orderCartApi/queryAllShopCart");
        return mv;

    }
}
